package com.zzf.cms.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzf.cms.entity.CrmBanner;
import com.zzf.cms.service.CrmBannerService;
import com.zzf.commonutils.R;
import com.zzf.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-04-14
 */
@Api(description = "轮播图后台管理")
@CrossOrigin
@RestController
@RequestMapping("/educms/banneradmin")
public class BannerAdminController {

    @Autowired
    private CrmBannerService bannerService;

    //分页查询banner
    @GetMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page, @PathVariable long limit) {

        Page<CrmBanner> pageBanner = new Page<>(page, limit);
        bannerService.page(pageBanner, null);
        HashMap<String, Object> map = new HashMap<>();

        map.put("items", pageBanner.getRecords());
        map.put("total", pageBanner.getTotal());

        return R.ok().data(map);
    }

    //添加banner
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner) {
        bannerService.save(crmBanner);
        return R.ok();
    }

    //修改
    @PostMapping("update")
    public R updateBanner(@RequestBody CrmBanner crmBanner) {
        bannerService.updateById(crmBanner);
        return R.ok();
    }

    @DeleteMapping("remove/{id}}")
    public R removeBanner(@PathVariable String id) {
        boolean remove = bannerService.removeById(id);
        if (remove) {
            return R.ok();
        } else {
            throw new GuliException(20001, "删除失败");
        }
    }

    //根据id查询
    @GetMapping("getBanner/{id}")
    public R getBannerById(@PathVariable String id) {
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("item", banner);
    }
}

